<?php
/* SVN FILE: $Id: fileedits.php 200 2009-09-03 18:45:33Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket ACP home page
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 200 $
* @lastrevision $Date: 2009-09-03 18:45:33 +0000 (Thu, 03 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-03 18:45:33 +0000 (Thu, 03 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/items/fileedits.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_items_fileedits extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'item_edit' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_items');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_items', 'admin_item_language' ) );
		
		$execute = array(
							'list' => 'listItems',
							'edits' => 'showEdits',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'list';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function listItems()
	{
		$itemsDirectory = array("");
		foreach(new DirectoryIterator(IPSLib::getAppDir('ibmarket')."/items") as $directory)
		{
			if($directory->isDir() && !$directory->isDot())
			{
				if(file_exists(IPSLib::getAppDir('ibmarket')."/items/".$directory->getFilename()."/info.php"))
				{
					$itemsDirectory[] = $directory->getFilename();
				}
			}
		}
		
		$itemTemplate = new ReflectionClass('templateItem');
		$directoryInfo = array();
		$itemsList = array();
		foreach($itemsDirectory as $directory)
		{
			$title = "";
			$description = "";
			require(IPSLib::getAppDir('ibmarket')."/items/".$directory."/info.php");
			if(!isset($title))
			{
				continue;
			}
			
			$directoryInfo[$directory]['title'] = isset($this->lang->words[$title]) ? $this->lang->words[$title] : $title;
			$directoryInfo[$directory]['description'] = isset($this->lang->words[$description]) ? $this->lang->words[$description] : $description;
			
			$itemsList[$directory] = array();
			$itemsList[$directory][] = array(-1,str_repeat("-",40));
			foreach(new DirectoryIterator(IPSLib::getAppDir('ibmarket')."/items/".$directory) as $file)
			{
				if(strstr($file->getFilename(),"item_") != $file->getFilename() || substr($file->getFilename(),-4) != '.php')
				{
					continue;
				}
				
				require_once(IPSLib::getAppDir('ibmarket')."/items/".$directory."/".$file->getFilename());
				
				$class = new ReflectionClass(marketRegistry::getClassName($file->getFilename()));
				if(!$class->isSubclassOf($itemTemplate))
				{
					continue;
				}
				
				$item = $class->newInstanceArgs(array(&$this->registry,array()));
				
				if(!$item->adminViewFileEdits())
				{
					continue;
				}
				$itemName = $item->getDefaultName();
				$itemsList[$directory][] = array($file->getFilename(),$itemName);
			}
		}
		
		$this->registry->output->html = $this->html->listItemFileEdit( $directoryInfo, $itemsList );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function showEdits()
	{
		$itemsDirectory = array("");
		foreach(new DirectoryIterator(IPSLib::getAppDir('ibmarket')."/items") as $directory)
		{
			if($directory->isDir() && !$directory->isDot())
			{
				if(file_exists(IPSLib::getAppDir('ibmarket')."/items/".$directory->getFilename()."/info.php"))
				{
					require(IPSLib::getAppDir('ibmarket')."/items/".$directory->getFilename()."/info.php");
					if(!isset($title))
					{
						continue;
					}
					$itemsDirectory[] = $directory->getFilename();
				}
			}
		}
		
		$itemTemplate = new ReflectionClass('templateItem');
		$item = null;
		$file = "";
		foreach($itemsDirectory as $directory)
		{
			if(!isset($this->request['directory'][$directory]) || $this->request['directory'][$directory] == -1)
			{
				continue;
			}
			if(!file_exists(IPSLib::getAppDir('ibmarket')."/items/".$directory."/".$this->request['directory'][$directory]))
			{
				continue;
			}
			if(strstr($this->request['directory'][$directory],"item_") != $this->request['directory'][$directory] || substr($this->request['directory'][$directory],-4) != '.php')
			{
				continue;
			}
			require_once(IPSLib::getAppDir('ibmarket')."/items/".$directory."/".$this->request['directory'][$directory]);
			
			$class = new ReflectionClass(marketRegistry::getClassName($this->request['directory'][$directory]));
			if(!$class->isSubclassOf($itemTemplate))
			{
				continue;
			}
			
			$_item = $class->newInstanceArgs(array(&$this->registry,array()));
			
			if(!$_item->adminViewFileEdits())
			{
				continue;
			}
			
			$item = $_item;
			break;
		}
		if(is_null($item))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=items&amp;section=fileedits" );
		}
		
		$this->registry->output->html = $this->html->itemFileEdit( $item );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
}